package com.xbts.screen.controller.screen;

import com.xbts.common.core.constant.CacheConstants;
import com.xbts.common.core.constant.Constants;
import com.xbts.common.core.enums.ConsumeTypeEnum;
import com.xbts.common.core.enums.DateTypeEnum;
import com.xbts.common.core.enums.WorkshopTypeEnum;
import com.xbts.common.core.utils.StringUtils;
import com.xbts.common.core.web.controller.BaseController;
import com.xbts.common.core.web.domain.AjaxResult;
import com.xbts.common.core.web.domain.BaseHistogramResult;
import com.xbts.common.security.annotation.RequiresPermissions;
import com.xbts.screen.domain.vo.ScreenConsumeHistogramVO;
import com.xbts.screen.service.ISyntheticScreenServer;
import com.xbts.screen.service.IWeVirtualDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 大屏-能耗
 */
@Api(tags = "大屏-能耗")
@RequestMapping("/home")
@RestController
public class ScreenConsumeController extends BaseController {

    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    private IWeVirtualDataService weVirtualDataService;
    @Autowired
    private ISyntheticScreenServer syntheticScreenServer;
    /**
     * 能耗-详情-按时间类型
     */
    @ApiOperation("能耗-详情-按时间类型")
    @RequiresPermissions("screen:consume:detailsByDateType")
    @GetMapping("/consumeDetailsByDateType")
    public AjaxResult consumeDetailsByDateType(DateTypeEnum dateType, ConsumeTypeEnum consumeType){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        if (StringUtils.equals(showVirtualData, Constants.TRUE)){
            return success(weVirtualDataService.consumeDetailsByDateType(dateType, consumeType));
        } else {
            BaseHistogramResult result = (BaseHistogramResult)redisTemplate.opsForValue().get(CacheConstants.SCREEN_CONSUME_DETAILS_BY_DATE_TYPE + "_" + dateType);
            if(result != null){
                return success(result);
            } else {
                result = syntheticScreenServer.consumeDetailsByDateType(dateType, consumeType);
                if(result != null) {
                    redisTemplate.opsForValue().set(CacheConstants.SCREEN_CONSUME_DETAILS_BY_DATE_TYPE + "_" + dateType, result, 15, TimeUnit.MINUTES);
                }
            }
            if(result == null){
                return success(weVirtualDataService.consumeDetailsByDateType(dateType, consumeType));
            }
            return success(result);
        }
    }
    /**
     * 能耗-车间详情-按时间类型
     */
    @ApiOperation("能耗-车间详情-按时间类型")
    @RequiresPermissions("screen:consume:workshop:detailsByDateType")
    @GetMapping("/consumeWorkshopDetailsByDateType")
    public AjaxResult consumeWorkshopDetailsByDateType(DateTypeEnum dateType, ConsumeTypeEnum consumeType, String workshopType){
        BaseHistogramResult result = syntheticScreenServer.consumeWorkshopDetailsByDateType(dateType, consumeType, workshopType);
        return success(result);
    }

}
